package leetcode3

func lengthOfLongestSubstring(s string) int {
	if len(s) <= 1 {
		return len(s)
	}
	var (
		charMap = map[byte]int{}
		max     = 1
		i       = 0
		j       = 1
	)
	// pwcswkew
	charMap[s[i]] = i
	for {
		max = maxInt(max, j-i)
		if j >= len(s) {
			break
		}
		// 已存在
		if _, ok := charMap[s[j]]; ok && charMap[s[j]] >= i {

			// i 往后移
			i = charMap[s[j]] + 1
		}
		// 不存在 写入map
		charMap[s[j]] = j
		j++
	}
	return max
}

func maxInt(x, y int) int {
	if x > y {
		return x
	}
	return y
}
